//
//  SBCheckVersionUpdate.m
//  sharedBooks
//
//  Created by XunFengKJ on 2017/7/14.
//  Copyright © 2017年 XunFengKJ. All rights reserved.
//

#import "SBCheckVersionUpdate.h"
@interface SBCheckVersionUpdate ()
@property(assign,nonatomic)BOOL isForceUpdate;
@end
@implementation SBCheckVersionUpdate

//更新 url下载地址,version_code版本号,updata_message更新信息,up是否强制更新0否1是
- (void)postUpdateVerson{
    WEAKSELF
    [XFPostRequest postGetVersionInfoByPlatform:@"ios" success:^(id responseObject) {
        LGFLog(@" === %@",responseObject);
        if ([[responseObject safeJsonObjectForKey:@"status"] integerValue] == 200) {
            //版本
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            // app Version版本
            NSString *str = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
            NSString *vsionStr = [[responseObject safeJsonObjectForKey:@"content"] safeJsonObjectForKey:@"version"];
            LGFLog(@"vsionStr%@--%@",vsionStr,str);
            //同一版本
            if ([vsionStr isEqualToString:str]) {

            }else{
                if(!weakSelf.isForceUpdate) {
                    weakSelf.isForceUpdate = YES;
                    NSInteger up = [[[responseObject safeJsonObjectForKey:@"content"] safeJsonObjectForKey:@"forceUpdate"] integerValue];
                    //强制更新
                    if (up == 0) {
                        [AlertView alertWithTitle:@"发现新版本" message:[NSString stringWithFormat:@"%@",[[responseObject safeJsonObjectForKey:@"content"] safeJsonObjectForKey:@"message"]] buttonTitle:@"立即更新" confrimBlock:^{
                            weakSelf.isForceUpdate = NO;
                            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[[responseObject safeJsonObjectForKey:@"content"] safeJsonObjectForKey:@"url"]]];
                        }];
                    }
                    //可以忽略
                    else{
                        [AlertView alertWithTitle:@"发现新版本" message:[NSString stringWithFormat:@"%@",[[responseObject safeJsonObjectForKey:@"content"] safeJsonObjectForKey:@"message"]] cancelTitle:@"以后再说" cancelBlock:^{

                        } confirmTitle:@"立即更新" confrimBlock:^{
                            weakSelf.isForceUpdate = NO;
                            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[[responseObject safeJsonObjectForKey:@"content"] safeJsonObjectForKey:@"url"]]];
                        }];
                    }
                }
            }
        }
    } failure:^(NSError *error) {
        LGFLog(@"%@",error);
    }];
}

@end
